<?php
/**
 *
 * Created by IntelliJ IDEA.
 * User: XD
 * Date: 2017/8/23
 * Time: 9:24
 */
 function checkNum($number){
     if($number>1)
     {
         throw new Exception("Value must be 1 or below");
     }
     return true;
 }

 //checkNum(2);

 try{
     checkNum(2);
     echo "如果输出该内容,说明 $number 变量小于1";
 }catch (Exception $e){
     echo "MEssage:".$e->getMessage();
 }

 class customException extends Exception
 {
     public function errorMessage(){
         // 错误信息
         $errorMsg = "错误行号".$this->getLine()."in".$this->getFile()
             .":<b>".$this->getMessage()."</b>不是一个合法的E-Mail地址";
         return $errorMsg;
     }
 }

 $email = "xujiang5@163.com";

 try
 {
     // 检测邮箱
     if(filter_var($email,FILTER_VALIDATE_EMAIL) === FALSE)
     {
         // 如果是个不合法的邮箱地址，抛出异常
         throw new customException($email);
     }
 }catch (customException $e) {
     echo $e->errorMessage();
 }
 // 设置顶层异常处理器
 function myException($exception){
     echo "<b>Exception:</b>",$exception->getMessage();
 }

 set_exception_handler("myException");

 throw new Exception('Uncaught Exception occurred');

 ?>